<?php
/*
$JA#COPYRIGHT$
*/

// no direct access
defined('_JEXEC') or die('Restricted access');


$user = JFactory::getUser();

// Include the syndicate functions only once
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'asset'.DS.'jaconstants.php');
require_once (dirname(__FILE__).DS.'helper.php');
modJaJobboard_Menu::Install_Menu_DB();

$type 	= modJaJobboard_Menu::getType();
$return	= modJaJobboard_Menu::getReturnURL($params, $type);

if($user->id || checkPermission('') ){

	// Set redirection page after login
	modJaJobboard_Menu::redirectToPage($params);
	
	// Include the syndicate functions only once
	$module_content = '';
	$jmodule				= new stdClass;
	$jmodule->id			= 0;
	$jmodule->title			= JText::_('Default Menu');
	$jmodule->module		= 'mod_mainmenu';
	$jmodule->name			= 'mainmenu';
	$jmodule->position		= '';
	$jmodule->content		= '';
	$jmodule->showtitle		= 0;
	$jmodule->control		= '';
	$jmodule->user			= 0;
		
	// Menu paramaters
	$jmenuclass_sfx		= $params->get("menuclass_sfx", "");
	$menu_style			= $params->get("menu_style", "list");
	$startLevel			= $params->get("startLevel", "0");
	$endLevel			= $params->get("endLevel", "0");
	$showAllChildren	= $params->get("showAllChildren", "0");
	$window_open		= $params->get("window_open", "");
	$show_whitespace	= $params->get("show_whitespace", "0");
	$module_cache		= $params->get("cache", "1");
	$tag_id				= $params->get("tag_id", "");
	$maxdepth			= $params->get("maxdepth", "10");
	$menu_images		= $params->get("maxdepth", "0");
	$menu_images_align	= $params->get("menu_images_align", "0");
	$menu_images_link	= $params->get("menu_images_link", "0");
	$expand_menu		= $params->get("expand_menu", "0");
	$activate_parent	= $params->get("activate_parent", "0");
	$full_active_id		= $params->get("full_active_id", "0");
	$full_active_id		= $params->get("full_active_id", "0");
	$indent_image		= $params->get("indent_image", "0");
	$indent_image1		= $params->get("indent_image1", "");
	$indent_image2		= $params->get("indent_image2", "");
	$indent_image3		= $params->get("indent_image3", "");
	$indent_image4		= $params->get("indent_image4", "");
	$indent_image5		= $params->get("indent_image5", "");
	$indent_image6		= $params->get("indent_image6", "");
	$spacer				= $params->get("spacer", "");
	$end_spacer			= $params->get("end_spacer", "");
	
	/*hack for jobboard component*/
	$jbmenutype = 'default-menus';
	$user 		= JFactory::getUser();
	$usertype 	= $user->getParam('usertype', '');

	// Check subscription
	$session =& JFactory::getSession();
    $checked = $session->get("jb_menu_".$user->id, 0);
    if(!$checked){
    	$db = JFactory::getDBO();
    	$query = "SELECT a.id as transactionid, b.id as subscriptionid, b.duration, b.units, a.created_date, a.user_id 
    				FROM #__ja_transactions a 
    				INNER JOIN #__ja_subscriptions b ON a.product_id = b.id 
    				WHERE a.user_id = ".$user->id." AND a.product_type = 'S' AND a.completed = '1'";
    	$db->setQuery($query);
    	$result = $db->loadObject();    	
    	if($result){
    		
    		if( $result->duration > 0 ){
    			$registered = strtotime($result->created_date);
    			$string = "";
    			switch ($result->units){
    				case "D":
    					$string = "day";
    					break;
    				case "M":
    					$string = "month";
    					break;
    				case "Y":
    					$string = "year";
    					break;
    			}
    			if( $result->duration > 1 ) $string = $string."s";    			
    			$registered = $registered + ( strtotime("+".$result->duration." ".$string) - time() );    			
    			if( time() > $registered ){
    				// Raise notice duration day
    				require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'models'.DS.'jasubscriptions.php');
    				$model = new JobBoardModelJASubscriptions();
    				$model->mailSubscriptionExpired($result->user_id, $result->subscriptionid, $result->transactionid);
    				JError::raiseNotice(1, JText::_("Your subscription is expired"));
					global $mainframe;
					$mainframe->logout();
					$session->destroy();
    				$user->id = 0;
    			}
    		}
    	}
    	$session->set("jb_menu_".$user->id, 1);
    }
	
	if( $usertype == 'Employer User' ) {
		$jbmenutype 	= 'employer-menus';
		$module->title 	= JText::_('Employer Menu');
	}
	elseif( $usertype == 'Job Seeker User' ){
		$jbmenutype 	= 'job-seeker-menus';
		$module->title 	= JText::_('Job Seeker Menu');
	}
		
	$jmodule->params	= "menutype=$jbmenutype\n".
							"menu_style=$menu_style\n".
							"startLevel=$startLevel\n".
							"endLevel=$endLevel\n".
							"showAllChildren=$showAllChildren\n".
							"window_open=$window_open\n".
							"show_whitespace=$show_whitespace\n".
							"cache=$module_cache\n".
							"tag_id=$tag_id\n".
							"class_sfx=$jmenuclass_sfx\n".
							"moduleclass_sfx=_menu\n".
							"maxdepth=$maxdepth\n".
							"menu_images=$menu_images\n".
							"menu_images_align=$menu_images_align\n".
							"menu_images_link=$menu_images_link\n".
							"expand_menu=$expand_menu\n".
							"activate_parent=$activate_parent\n".
							"full_active_id=$full_active_id\n".
							"indent_image=$indent_image\n".
							"indent_image1=$indent_image1\n".
							"indent_image2=$indent_image2\n".
							"indent_image3=$indent_image3\n".
							"indent_image4=$indent_image4\n".
							"indent_image5=$indent_image5\n".
							"indent_image6=$indent_image6\n".
							"spacer=$spacer\n".
							"end_spacer=$end_spacer\n";
	if($user->id)
		$module_content 	= JModuleHelper::renderModule($jmodule, array());
	else $module_content = "";
}
elseif( $params->def('showLoginForm', 0) ){			
	// $module->title = JText::_('JB Login');
}

require(JModuleHelper::getLayoutPath('mod_jajobboard_menu'));